﻿using System.Application;
using System.Domain.Entity;
using System.Domain.IRepository;
using System.Repository;
using Application.Infrastructure;
using Domain.Infrastructure;
using Microsoft.AspNetCore.Mvc;
using Ubiquity.Core.Extend;
using Ubiquity.Core.Web;
using Ubiquity.Web.Filters;

namespace Ubiquity.Web.Areas.SystemManage.Controllers
{
    /// <summary>
    /// 角色
    /// </summary>
    [Area("SystemManage")]
    public class SysRoleController : BaseController
    {
        private readonly SysRoleService _service;

     
        public SysRoleController(SysRoleService service)
        {
            _service = service;

        }

        [HttpGet]
        [Authorize]
        public IActionResult GetGridPaginationJson(WebPagination pagination)
        {
            Pagination paging = pagination.Change();

            var data = new
            {
                rows = _service.GetList(paging),
                pagination.total,
                page = pagination.page,
                pagination.records
            };
            return Content(data.ToJson());
        }

        [HttpGet]
        public IActionResult GetGridJson()
        {
            var data = _service.GetList();
            return Content(data.ToJson());
        }
        [HttpGet]
        public ActionResult GetFormJson(string keyValue)
        {
            var data = _service.GetEntity(keyValue);
            return Content(data.ToJson());
        }
        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public IActionResult Insert(SysRoleEntity entity, string permissionIds, string keyValue)
        {
            //string dd = Request.Params["permissionIds"];
            return _service.Insert(entity, permissionIds.Split(',')) ? Success("操作成功。") : Error("操作失败。");

        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public IActionResult Update(SysRoleEntity entity, string permissionIds, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Id = keyValue;
                return _service.Update(entity, permissionIds.Split(',')) ? Success("操作成功。") : Error("操作失败。");
            }
            return Error("主键验证错误");
        }
        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public IActionResult DeleteForm(string keyValue)
        {
            try
            {
                return _service.Delete(keyValue) ? Success("操作成功。") : Error("操作失败。");
            }
            catch (System.Exception)
            {

                return Error("请查看改角色是否被用户引用");
            }
        }
    }

}
